package com.example.anew.journey.mynewjourney.customview;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.OvershootInterpolator;

/**
 * 杭州惟君医疗科技有限公司17
 * Created by 20433 on 2018/3/21
 *
 * @desc 东风山河绿，春已不折柳，繁花看不尽，处处惹人留
 * @desc 佛祖镇楼，bug辟容易
 */

public class MyCardiacProgessView extends View {
    private int totalRange; //总量程
    private int controlswidth; //控件宽
    private int controlsheight; //控件高
    private int xOffset = 40; //x轴偏移量
    private int progressHigh; //进度高
    private Paint tooSlowpaint;
    private int averagedistancesegment; //平均每段的长度
    private Context mcontext;
    private Paint normalPaint;
    private Paint toofast;
    private int progressOutsideCircleRadius ; //进度外圆半径
    private int  progressSmallCircleRadius;  //进度小内圆半径
    private Paint smallPaint;
    private int progressRange = 300;//进度量程

    public int getProgress() {
        return progress;
    }

    public void setProgress(int progress) {
        this.progress = progress;
        invalidate();
    }

    private int progress = 0; //进度

    public MyCardiacProgessView(Context context) {
        this(context,null);
    }
    public MyCardiacProgessView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }
    public MyCardiacProgessView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mcontext = context;
        init();
    }
    private void init() {
        // 过慢 画笔
        tooSlowpaint = new Paint();
        tooSlowpaint.setStyle(Paint.Style.FILL);
        tooSlowpaint.setAntiAlias(true);
        tooSlowpaint.setColor(0xffFFA500); //黄色

        //正常 画笔
        normalPaint = new Paint();
        normalPaint.setStyle(Paint.Style.FILL);
        normalPaint.setAntiAlias(true);
        normalPaint.setColor(0xff00CD66); //绿色

        //过快 画笔
        toofast = new Paint();
        toofast.setStyle(Paint.Style.FILL);
        toofast.setAntiAlias(true);
        toofast.setColor(0xffff0000); //红色

        smallPaint = new Paint();
        smallPaint.setStyle(Paint.Style.FILL);
        smallPaint.setAntiAlias(true);
        smallPaint.setColor(0xffffffff); //红色
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        controlswidth =w;
        controlsheight = h;
        progressHigh = controlsheight/3;
        averagedistancesegment = (controlswidth-2*xOffset)/5;
        progressOutsideCircleRadius = controlsheight/3;
        progressSmallCircleRadius = progressOutsideCircleRadius -2;
        super.onSizeChanged(w, h, oldw, oldh);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // todo 画圆角矩阵
        drawRoundedMatrix(canvas);

        //todo 进度画圆
        drawProgressCircle(canvas);
    }

    private void drawProgressCircle(Canvas canvas) {
        canvas.drawCircle(xOffset+(controlswidth-2*xOffset)/progressRange*progress,progressHigh+progressHigh/2,progressOutsideCircleRadius,normalPaint);
        canvas.drawCircle(xOffset+(controlswidth-2*xOffset)/progressRange*progress,progressHigh+progressHigh/2,progressSmallCircleRadius,smallPaint);
    }

    private void drawRoundedMatrix(Canvas canvas) {
       // canvas.drawRoundRect(xOffset,progressHigh,controlswidth -xOffset,controlsheight- progressHigh,50,50,tooSlowpaint);
        canvas.drawRect(xOffset,progressHigh,averagedistancesegment+xOffset,controlsheight- progressHigh,tooSlowpaint);
        canvas.drawCircle(xOffset,progressHigh+progressHigh/2,progressHigh/2,tooSlowpaint);

        canvas.drawRect(averagedistancesegment+xOffset,progressHigh,4*averagedistancesegment +xOffset,controlsheight- progressHigh,normalPaint);

        canvas.drawRect(4*averagedistancesegment +xOffset,progressHigh,5*averagedistancesegment+xOffset,controlsheight- progressHigh,toofast);
        canvas.drawCircle(5*averagedistancesegment+xOffset,progressHigh+progressHigh/2,progressHigh/2,toofast);
    }

    public void start(int mprogress){
        // 创建 ObjectAnimator 对象
        ObjectAnimator animator = ObjectAnimator.ofInt(MyCardiacProgessView.this, "progress", 0, mprogress);
        animator.setInterpolator(new OvershootInterpolator());
        animator.setDuration(2000);
        // 执行动画
        animator.start();
    }
}
